import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Supermarket implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name; // 超市名称
    private String address; // 地址
    private List<Member> members; // 会员列表

    public Supermarket(String name, String address) {
        this.name = name;
        this.address = address;
        this.members = new ArrayList<>();
    }

    public void addMember(String cardNumber, String name) {
        members.add(new Member(cardNumber, name));
        System.out.println("会员开卡成功，卡号: " + cardNumber);
    }

    public Member findMember(String cardNumber) {
        for (Member member : members) {
            if (member.getCardNumber().equals(cardNumber)) {
                return member;
            }
        }
        return null; // 找不到会员
    }

    public void showMembers() {
        if (members.isEmpty()) {
            System.out.println("没有会员信息。");
        } else {
            for (Member member : members) {
                System.out.println(member);
            }
        }
    }

    public void saveData() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("supermarket.dat"))) {
            oos.writeObject(this);
            System.out.println("数据已保存。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Supermarket loadData() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("supermarket.dat"))) {
            return (Supermarket) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("没有找到数据文件，创建新超市。");
            return null;
        }
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public List<Member> getMembers() {
        return members;
    }
}
